/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;

import com.bourre.core.HashCodeFactory;
import com.bourre.data.collections.Map;
import com.bourre.utils.ClassUtils;

import fever.app.memento.CareTaker;
import fever.app.memento.Memento;
import fever.app.memento.Originator;
import fever.exception.MementoException;

import fvaswing.memento.FvComponentOriginator;

/**
 * State manager for <strong>Aswing</strong> components.
 * 
 * <p>Use the {@link #saveState()} method to save component state, and use 
 * {@link #getState()} to retreive specific one.
 * 
 * <p>Also use {@link #undo()} or {@link #redo()} methods.
 * 
 * @see fvaswing.memento.FvComponentMemento
 * @see fvaswing.memento.FvComponentOriginator
 * 
 * @author Romain Ecarnot
 */
class fvaswing.memento.FvAswingState
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _takerMap : Map = new Map();
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Inits state engine for passed-in {@code component} component with 
	 * {@code originator}.
	 * 
	 * <p>If {@link fever.app.memento.Originator} is {@code null} or 
	 * {@code undefined}, use the default {@link fvaswing.memento.FvComponentOriginator}
	 * 
	 * <p>Component state is automatically save for the first time.
	 * 
	 * @param component AsWing compoenent to register
	 * @param originator {@link fever.app.memento.Originator} type to 
	 * manage passed-in component state.
	 */
	public static function init( component : Component, originator : Originator ) : Boolean
	{
		var id : String = _getID( component );
		
		if( !component ) return false;
		
		if( !_takerMap.containsKey( id ) )
		{
			if( !originator ) originator = new FvComponentOriginator( component );
			else
			{
				var sOriginatorName : String = ClassUtils.getFullyQualifiedClassName( originator );
				originator = HashCodeFactory.buildInstance( sOriginatorName, [component] );
			}
			
			var taker : CareTaker = new CareTaker( originator );
			var memento : Memento = originator.createMemento();
			
			_takerMap.put( id, taker );
			taker.push( memento );
			
			return true;
		}
		else return false;
	}
	
	/**
	 * Saves the state of passed-in {@code component} component.
	 * 
	 * @param component Component target
	 * 
	 * @return {@link fvaswing.memento.FvComponentMemento} state.
	 * 
	 * @throws {@link fever.exception.MementoException} if passed-in 
	 * {@code component} is not already registred in state manager.
	 * 
	 * @see #init()
	 */
	public static function saveSate( component : Component ) : Memento
	{
		var id : String = _getID( component );
		
		if( _takerMap.containsKey( id ) )
		{
			var taker : CareTaker = _takerMap.get( id );
			var memento : Memento = taker.getOriginator().createMemento();
			taker.push( memento );
			return memento;	
		}
		else
		{
			throw new MementoException( component.getName() + " is not registred" );
			return null;
		}
	}
	
	/**
	 * Retreives ans applies passed-in {@link fever.app.memento.Memento} 
	 * to {@code component}
	 * 
	 * @param component Component target
	 * @param memento Stage to restore
	 * 
	 * @return {@link fvaswing.memento.FvComponentMemento} state.
	 * 
	 * @throws {@link fever.exception.MementoException} 
	 * if passed-in {@code component} is not already registred in state manager.
	 * 
	 * @see #init()
	 */
	public static function getState( component : Component, memento : Memento ) : Memento
	{
		var id : String = _getID( component );
		
		if( _takerMap.containsKey( id ) )
		{
			var originator : FvComponentOriginator = FvComponentOriginator( CareTaker( _takerMap.get( id ) ).getOriginator() );
			originator.setMemento( memento );
			return memento;
		}
		else
		{
			throw new MementoException( component.getName() + " is not registred" );
			return null;
		}
	}
	
	/**
	 * Try an {@code undo} operation on {@code component} state.
	 * 
	 * @param component Component target
	 * 
	 * @throws {@link fever.exception.MementoException} if passed-in 
	 * {@code component} is not already registred in state manager.
	 * 
	 * @throws {@link fever.exception.MementoException} if no undo state 
	 * is available.
	 */
	public static function undo( component : Component ) : Void
	{
		var id : String = _getID( component );
		
		if( _takerMap.containsKey( id ) )
		{
			var taker : CareTaker = _takerMap.get( id );
			try
			{
				FvComponentOriginator( taker.getOriginator() ).setMemento( taker.undo() );
			}
			catch( e : MementoException )
			{
				e.message = "No undo state for " +  taker.getOriginator();
				throw e;	
			}	
		}
		else throw new MementoException( component.getName() + " is not registred" );
	}
	
	/**
	 * Try an {@code redo} operation on {@code component} state.
	 * 
	 * @param component Component target
	 * 
	 * @throws {@link fever.exception.MementoException} if passed-in 
	 * {@code component} is not already registred in state manager.
	 * 
	 * @throws {@link fever.exception.MementoException} if no redo state 
	 * is available.
	 */
	public static function redo( component : Component ) : Void
	{
		var id : String = _getID( component );
		
		if( _takerMap.containsKey( id ) )
		{
			var taker : CareTaker = _takerMap.get( id );
			try
			{
				taker.getOriginator().setMemento( taker.redo() );
			}
			catch( e : MementoException )
			{
				e.message = "No redo state for " + taker.getOriginator();
				throw e;
			}
		}
		else throw new MementoException( component.getName() + " is not registed" );
	}	
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private static function _getID( component : Component ) : String
	{
		return String( component.getID() );	
	}
	
	
	/**
	 * Constructor.
	 */
	private function FvAswingState()
	{
		
	}			
}